/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/DrawControls/TextList.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Artyomov Denis, Vladimir Kryukov
 *******************************************************************************/


package DrawControls;

import DrawControls.text.*;
import java.util.*;
import jimm.ui.base.*;
import jimm.ui.menu.*;


/**
 * Text list.
 * 
 * This class store text and data of lines internally.
 * You may use it to show text with colorised lines :)
 */
public abstract class TextList extends VirtualList {
    protected final Vector pars = new Vector();
    protected int defaultCode = -1;

    protected final Parser createParser(int index) {
        final int width = NativeCanvas.getInstance().getMinScreenMetrics()
                - scrollerWidth - 3;
        return new Parser(index, getFontSet(), width);
    }
    protected final void addPar(Parser item) {
        pars.addElement(item.getPar());
    }

    protected final Par getPar(int index) {
        return (Par) pars.elementAt(index);
    }

    public final int getSize() {
        return pars.size();
    }

    protected final boolean isItemSelected(int index) {
        int selIndex = getCurrItem();
        int textIndex = (selIndex >= getSize()) ? -1 : getPar(selIndex).index;
        if (textIndex == -1) {
            return false;
        }
        return (getPar(index).index == textIndex);
    }

    //! Construct new text list with default values of colors, font size etc...
    public TextList(String capt) {
        super(capt);
        setSoftBarLabels("menu", null, "back", false);
    }

    public final int getItemHeight(int itemIndex) {
        if (itemIndex >= getSize()) {
            return 1;
        }
        return getPar(itemIndex).getHeight();
    }

    // Overrides VirtualList.drawItemData
    protected final void drawItemData(GraphicsEx g, int index, int x1, int y1, int w, int h, int skip, int to) {
        getPar(index).paint(getFontSet(), g, 1, y1, skip, to);
    }

    /** Remove all lines form list */
    public void clear() {
        pars.removeAllElements();
        setCurrentItemToTop(0);
        invalidate();
    }
    
    protected final void setDefaultCode(int def) {
        defaultCode = def;
    }
    protected abstract MenuModel getMenu();

    protected void doKeyReaction(int keyCode, int actionCode, int type) {
        if (type == KEY_PRESSED) {
            switch (actionCode) {
                case NativeCanvas.LEFT_SOFT:
                    showMenu(getMenu());
                    return;
                    
                case NativeCanvas.RIGHT_SOFT:
                case NativeCanvas.CLOSE_KEY:
                    backAct();
                    return;
            }
        }
        super.doKeyReaction(keyCode, actionCode, type);
    }

    private void backAct() {
        MenuModel menu = getMenu();
        if (null == menu) {
            back();
        } else {
            menu.exec(null, MenuModel.BACK_COMMAND_CODE);
        }
    }    
}